<template>
	<view class="content">
		<view v-for="jihua in jihuas" :key="jihua.id" class="overdue" @click="goPage(jihua.id)">
			<view class="jihua-title">
				{{jihua.title}}
			</view>
			<view class="jihua-content">
				{{jihua.content}}
			</view>
			<view class="jihua-value">
				<view class="jihua-teach">
					发起人:
					<view class="tea">{{jihua.teacherName}}</view>
				</view>
				<view class="jihua-date">
					<view class="date">{{jihua.rekedateTime}}</view>-<view class="date">{{jihua.expirdateTime}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				jihuas: []
			}
		},
		methods: {
			goPage(id) {
				console.log(id);
				if (id == 0) {
					uni.switchTab({
						url: '/pages/login/login'
					})
				} else if (id >= 1) {
					uni.navigateTo({
						url: '/pages/jihuadeta/jihuadeta?id=' + encodeURIComponent(id.toString())
					})
				} else {
					uni.navigateTo({
						url: '/pages/jihuaAdd/jihuaAdd'
					})
				}
			},
			onPullDownRefresh() {
				console.log('刷新页面');
				setTimeout(() => {
					uni.stopPullDownRefresh();
					this.showAllZuoyes();
				}, 1000)
			},
			getJwtToken() {
				try {
					return wx.getStorageSync('jwt_token');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 假设你使用 uni.request 发送请求  
			sendRequestWithToken(url, method) {
				const token = this.getJwtToken();
				if (token) {
					wx.request({
						url: url,
						method: method,
						header: {
							'token': token // 将令牌添加到 Authorization 头部  
						},
						success: (res) => {
							if (res.data.code === 1 && res.data.msg === 'success') {
								let list = res.data.data;
								list.forEach(item => {
									item.rekedateTime = item.rekedateTime.replace('T', ' ');
									item.expirdateTime = item.expirdateTime.replace('T', ' ');
								});
								this.jihuas = list;

							} else if (res.data.code === 0) {
								uni.showToast({
									title: '登录信息有误',
									icon: null
								});
								this.goPage(0);
								console.error('未登录', res.data.data);
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '网络请求失败',
								icon: 'none'
							});
							console.error('请求失败', error);
						}
					});
				} else {
					this.goPage(0);
					// 如果没有令牌，处理没有令牌的情况，比如跳转到登录页  
					console.error('No token found, please login first.');
				}
			},
			getuserName() {
				try {
					return wx.getStorageSync('user_name');
				} catch (e) {
					// 处理获取令牌失败的逻辑，比如返回 null 或抛出错误  
					return null;
				}
			},
			// 在某个页面的事件处理函数中  
			showAllZuoyes() {
				const username = this.getuserName();
				if (username) {
					const url = `http://localhost:8080/patra/plan/${username}`;
					const method = 'GET';
					this.sendRequestWithToken(url, method);
				} else {
					console.error("用户名获取失败")
				}

			},
			onLoad() {
				this.showAllZuoyes();
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 100%;
		margin: 0%;
		padding: 0%;
	}

	.overdue {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		flex-direction: column;
		width: 90%;
		height: auto;
		margin-left: 0rpx;
		margin-top: 50rpx;
		background-color: #F3F3F3;
		border-radius: 25rpx;
	}

	.jihua-title {
		display: flex;
		align-items: center;
		font-family: AliMamaShuHei;
		width: 500rpx;
		height: 80rpx;
		margin-left: 30rpx;
	}

	.jihua-content {
		display: flex;
		width: 90%;
		height: auto;
		align-items: center;
		justify-content: center;
		margin: 0% 5% 0% 5%;
		font-size: 28rpx;

	}

	.jihua-value {
		display: flex;
		justify-content: space-between;
		height: 40rpx;
		margin-left: 30rpx;
		margin-top: 30rpx;
	}

	.jihua-teach {
		display: flex;
		width: 200rpx;
		height: 40rpx;
		justify-items: center;
		font-size: 23rpx;
		font-weight: bold;
		font-family: AliMamaShuHei;
	}

	.jihua-date {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 40rpx;
		width: 450rpx;
		font-size: 18rpx;
		font-weight: bold;
		font-family: AliMamaShuHei;
	}

	.tea {
		margin-left: 20rpx;
	}

	.date {
		margin-left: 10rpx;
		margin-right: 10rpx;
	}

	.addplan {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 300rpx;
		height: 70rpx;
		border-radius: 30rpx;
		margin-top: 70rpx;
		margin-bottom: 100rpx;
	}

	.addplan-button {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 300rpx;
		height: 70rpx;
		border-radius: 50rpx;
		background-color: #585CE5;
		font-family: AliMamaShuHei;
		color: #F3F3F3;
		font-size: 28rpx;

	}
</style>